package com.hm.hall.commons.config;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.sql.DriverManager;
import java.sql.SQLException;

@Component
public class MyConnection {
    private static Logger log = Logger.getLogger(MyConnection.class);
    private static java.sql.Connection sqlConnection;

    public static java.sql.Connection getConnection() {
        String driver = RDSConnection.driver;
        String url = RDSConnection.url;
        String user = RDSConnection.user;
        String pass = RDSConnection.pass;

        if (sqlConnection != null) return sqlConnection;

        try {
            Class.forName(driver);
            sqlConnection = DriverManager.getConnection(url, user, pass);
        } catch (ClassNotFoundException | SQLException e) {
            log.error(e.getMessage());
        }

        return sqlConnection;
    }

    public static void closeConnection(java.sql.Connection sqlConnection) {
        try {
            sqlConnection.close();
        } catch (SQLException e) {
            log.error(e.getMessage());
        }
    }
}
